package com.gddb.xwj.mywiki.controller;

import com.gddb.xwj.mywiki.domain.Test;
import com.gddb.xwj.mywiki.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：test
 *
 * @author xwj
 * @create 2022-03-18 19:17
 */
@RestController
@Slf4j
public class TestController {

    @Resource
    private RedisTemplate redisTemplate;

    //my.hello:TEST 读不到配置则取默认值test
    @Value("${my.hello:TEST}")
    private String helo;

    @RequestMapping(value = "/hello" ,method = RequestMethod.GET)
    public String hello(){
        return "hello  :" + helo;
    }

    @Resource
    private TestService testService;

    @GetMapping("/test")
    public List<Test> list(){
        return testService.list();
    }

    @RequestMapping("/redis/set/{key}/{value}")
    public String set(@PathVariable String key, @PathVariable String value) {
        redisTemplate.opsForValue().set(key, value, 3600, TimeUnit.SECONDS);
        log.info("key: {}, value: {}", key, value);
        return "success";
    }

    @RequestMapping("/redis/get/{key}")
    public Object get(@PathVariable String key) {
        Object object = redisTemplate.opsForValue().get(key);
        log.info("key: {}, value: {}", key, object);
        return object;
    }
}
